
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Poolboy · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="benchee.html" />
    
    
    <link rel="prev" href="guardian.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="guardian.html">
            
                <a href="guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="5.2" data-path="poolboy.html">
            
                <a href="poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="benchee.html">
            
                <a href="benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="bypass.html">
            
                <a href="bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="distillery.html">
            
                <a href="distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="stream-data.html">
            
                <a href="stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Poolboy</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x5982;&#x679C;&#x4E0D;&#x63A7;&#x5236;&#x597D;&#x7A0B;&#x5E8F;&#x521B;&#x5EFA;&#x7684;&#x6700;&#x5927;&#x5E76;&#x884C;&#x8FDB;&#x7A0B;&#x6570;&#xFF0C;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#x5F88;&#x5BB9;&#x6613;&#x5C31;&#x4F1A;&#x8017;&#x5C3D;&#x3002;<a href="https://github.com/devinus/poolboy" target="_blank">Poolboy</a> &#x5C31;&#x662F;&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x5728; Erlang &#x4E0B;&#x88AB;&#x5E7F;&#x6CDB;&#x4F7F;&#x7528;&#x7684;&#x8F7B;&#x91CF;&#x7EA7;&#xFF0C;&#x901A;&#x7528;&#x8FDB;&#x7A0B;&#x6C60;&#x7A0B;&#x5E8F;&#x5E93;&#x3002;  </p>
<h2 id="&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;-poolboy&#xFF1F;">&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981; Poolboy&#xFF1F;</h2>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x8003;&#x8651;&#x4E00;&#x4E0B;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;&#x4F60;&#x7684;&#x4EFB;&#x52A1;&#x662F;&#x6253;&#x9020;&#x4E00;&#x4E2A;&#x4FDD;&#x5B58;&#x7528;&#x6237;&#x8D44;&#x6599;&#x5230;&#x6570;&#x636E;&#x5E93;&#x7684;&#x5E94;&#x7528;&#x3002;&#x5982;&#x679C;&#x4F60;&#x4E3A;&#x6BCF;&#x4E2A;&#x7528;&#x6237;&#x7684;&#x6CE8;&#x518C;&#x64CD;&#x4F5C;&#x90FD;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#xFF0C;&#x6700;&#x7EC8;&#x53EF;&#x80FD;&#x4F1A;&#x5BFC;&#x81F4;&#x5927;&#x91CF;&#x7684;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;&#x88AB;&#x521B;&#x5EFA;&#x51FA;&#x6765;&#x3002;&#x5230;&#x4E86;&#x67D0;&#x4E00;&#x65F6;&#x523B;&#xFF0C;&#x5B83;&#x53EF;&#x80FD;&#x5C31;&#x8D85;&#x8FC7;&#x4E86;&#x6570;&#x636E;&#x5E93;&#x7684;&#x627F;&#x8F7D;&#x80FD;&#x529B;&#x3002;&#x6700;&#x7EC8;&#xFF0C;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x4E5F;&#x4F1A;&#x629B;&#x51FA;&#x8FDE;&#x63A5;&#x8D85;&#x65F6;&#xFF0C;&#x548C;&#x5176;&#x5B83;&#x5404;&#x79CD;&#x5F02;&#x5E38;&#x3002;  </p>
<p>&#x89E3;&#x51B3;&#x7684;&#x65B9;&#x6848;&#x662F;&#xFF0C;&#x4F7F;&#x7528;&#x4E00;&#x7EC4; worker &#x8FDB;&#x7A0B;&#x6765;&#x9650;&#x5236;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x4E3A;&#x6BCF;&#x4E00;&#x4E2A;&#x7528;&#x6237;&#x6CE8;&#x518C;&#x64CD;&#x4F5C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x3002;&#x8FD9;&#x6837;&#x5C31;&#x80FD;&#x907F;&#x514D;&#x8017;&#x5C3D;&#x7CFB;&#x7EDF;&#x7684;&#x8D44;&#x6E90;&#x3002;  </p>
<p>Poolboy &#x5C31;&#x662F;&#x4E3A;&#x6B64;&#x4EA7;&#x751F;&#x3002;&#x5B83;&#x5141;&#x8BB8;&#x4F60;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x53D7; <code>Supervisor</code> &#x7BA1;&#x7406;&#x7684; worker &#x8FDB;&#x7A0B;&#x6C60;&#xFF0C;&#x5E76;&#x4E14;&#x8FD8;&#x4E0D;&#x9700;&#x8981;&#x82B1;&#x8D39;&#x4F60;&#x592A;&#x591A;&#x7684;&#x7CBE;&#x529B;&#x53BB;&#x7BA1;&#x7406;&#x3002;&#x5F88;&#x591A;&#x7A0B;&#x5E8F;&#x5E93;&#x7684;&#x5E95;&#x5C42;&#x90FD;&#x4F7F;&#x7528;&#x4E86; Poolboy&#x3002;&#x6BD4;&#x5982;&#xFF0C;<code>postgrex</code> &#x7684;&#x8FDE;&#x63A5;&#x6C60;&#x7BA1;&#x7406; <em>&#xFF08;Ecto &#x8FDE;&#x63A5; PostgreSQL &#x65F6;&#x4F7F;&#x7528;&#x7684;&#x7A0B;&#x5E8F;&#x5E93;&#xFF09;</em>&#x548C; <code>redis_poolex</code> <em>(Redis &#x8FDE;&#x63A5;&#x6C60;)</em> &#x90FD;&#x662F;&#x51FA;&#x540D;&#x7684;&#xFF0C;&#x4F7F;&#x7528;&#x4E86; Poolboy &#x7684;&#x7A0B;&#x5E8F;&#x5E93;&#x3002;  </p>
<h2 id="&#x5B89;&#x88C5;">&#x5B89;&#x88C5;</h2>
<p>&#x901A;&#x8FC7; mix&#xFF0C;&#x5B89;&#x88C5;&#x7B80;&#x76F4;&#x6613;&#x5982;&#x53CD;&#x638C;&#x3002;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x505A;&#x7684;&#x5C31;&#x662F;&#x628A; Poolboy &#x6DFB;&#x52A0;&#x5230; <code>mix.exs</code> &#x7684;&#x4F9D;&#x8D56;&#x914D;&#x7F6E;&#x91CC;&#x9762;&#x3002;  </p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x5148;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#xFF1A;  </p>
<pre><code class="lang-shell">$ mix new poolboy_app --sup
</code></pre>
<p>&#x628A; Poolboy &#x6DFB;&#x52A0;&#x5230; <code>mix.exs</code> &#x7684;&#x4F9D;&#x8D56;&#x914D;&#x7F6E;&#x91CC;&#x9762;&#x3002;  </p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">deps</span></span> <span class="hljs-keyword">do</span>
  [{<span class="hljs-symbol">:poolboy</span>, <span class="hljs-string">&quot;~&gt; 1.5.1&quot;</span>}]
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x7136;&#x540E;&#x5B89;&#x88C5;&#x83B7;&#x53D6;&#x4F9D;&#x8D56;&#x3002;  </p>
<pre><code class="lang-shell">$ mix deps.get
</code></pre>
<h2 id="&#x914D;&#x7F6E;&#x9009;&#x9879;">&#x914D;&#x7F6E;&#x9009;&#x9879;</h2>
<p>&#x4F7F;&#x7528; Poolboy &#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x662F;&#x8981;&#x4E86;&#x89E3;&#x4E00;&#x4E9B;&#x5B83;&#x7684;&#x914D;&#x7F6E;&#x9009;&#x9879;&#x3002;  </p>
<ul>
<li><code>:name</code> - &#x8FDB;&#x7A0B;&#x6C60;&#x540D;&#x5B57;&#x3002;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF08;Scope&#xFF09;&#x53EF;&#x4EE5;&#x662F; <code>:local</code>&#xFF0C;<code>:global</code> &#x6216;&#x8005; <code>:via</code>&#x3002;  </li>
<li><code>:worker_module</code> - &#x4EE3;&#x8868; worker &#x8FDB;&#x7A0B;&#x7684;&#x6A21;&#x5757;&#x3002;  </li>
<li><code>:size</code> - &#x6700;&#x5927;&#x8FDB;&#x7A0B;&#x6570;&#x3002;  </li>
<li><code>:max_overflow</code> - &#x5F53;&#x8FDB;&#x7A0B;&#x6C60;&#x4E3A;&#x7A7A;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x521B;&#x9020;&#x7684;&#x6700;&#x5927;&#x4E34;&#x65F6;&#x8FDB;&#x7A0B;&#x6570;&#x3002;&#xFF08;&#x53EF;&#x9009;&#xFF09;  </li>
<li><code>:strategy</code> - <code>:lifo</code> &#x6216; <code>:fifo</code>&#xFF0C;&#x51B3;&#x5B9A;&#x4E86;&#x56DE;&#x6536;&#x5230;&#x8FDB;&#x7A0B;&#x6C60;&#x7684; worker &#x8FDB;&#x7A0B;&#xFF0C;&#x662F;&#x653E;&#x5230;&#x53EF;&#x7528; worker &#x8FDB;&#x7A0B;&#x961F;&#x5217;&#x7684;&#x5F00;&#x5934;&#x8FD8;&#x662F;&#x7ED3;&#x5C3E;&#x3002;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A; <code>:lifo</code>&#x3002;&#xFF08;&#x53EF;&#x9009;&#xFF09;</li>
</ul>
<h2 id="&#x5F00;&#x59CB;&#x914D;&#x7F6E;-poolboy">&#x5F00;&#x59CB;&#x914D;&#x7F6E; Poolboy</h2>
<p>&#x4EE5;&#x4E0B;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8D1F;&#x8D23;&#x5904;&#x7406;&#x8BA1;&#x7B97;&#x5E73;&#x65B9;&#x6839;&#x8BF7;&#x6C42;&#x7684; worker &#x8FDB;&#x7A0B;&#x6C60;&#x3002;&#x6837;&#x4F8B;&#x4F1A;&#x5C3D;&#x91CF;&#x7B80;&#x5355;&#x4EE5;&#x4FBF;&#x4E8E;&#x6211;&#x4EEC;&#x5173;&#x6CE8;&#x5728; Poolboy &#x4E0A;&#x4E3A;&#x4E3B;&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x5148;&#x914D;&#x7F6E; Poolboy&#xFF0C;&#x5E76;&#x628A; Poolboy worker &#x8FDB;&#x7A0B;&#x6C60;&#x6DFB;&#x52A0;&#x5230;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x4E2D;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x5B50;&#x8FDB;&#x7A0B;&#x3002;&#x7F16;&#x8F91; <code>lib/poolboy_app/application.ex</code> &#x5982;&#x4E0B;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">PoolboyApp</span></span>.Application <span class="hljs-keyword">do</span>
  <span class="hljs-variable">@moduledoc</span> <span class="hljs-keyword">false</span>

  <span class="hljs-keyword">use</span> Application

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">poolboy_config</span></span> <span class="hljs-keyword">do</span>
    [
      {<span class="hljs-symbol">:name</span>, {<span class="hljs-symbol">:local</span>, <span class="hljs-symbol">:worker</span>}},
      {<span class="hljs-symbol">:worker_module</span>, PoolboyApp.Worker},
      {<span class="hljs-symbol">:size</span>, <span class="hljs-number">5</span>},
      {<span class="hljs-symbol">:max_overflow</span>, <span class="hljs-number">2</span>}
    ]
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start</span></span>(_type, _args) <span class="hljs-keyword">do</span>
    children = [
      <span class="hljs-symbol">:poolboy</span>.child_spec(<span class="hljs-symbol">:worker</span>, poolboy_config())
    ]

    opts = [<span class="hljs-symbol">strategy:</span> <span class="hljs-symbol">:one_for_one</span>, <span class="hljs-symbol">name:</span> PoolboyApp.Supervisor]
    Supervisor.start_link(children, opts)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6700;&#x5F00;&#x59CB;&#x5B9A;&#x4E49;&#x7684;&#x662F;&#x8FDB;&#x7A0B;&#x6C60;&#x7684;&#x914D;&#x7F6E;&#x9009;&#x9879;&#x3002;&#x8FDB;&#x7A0B;&#x6C60;&#x7684;&#x540D;&#x5B57;&#x8BBE;&#x7F6E;&#x4E3A; <code>:worker</code>&#xFF0C;<code>:scope</code> &#x8BBE;&#x4E3A; <code>:local</code>&#x3002;&#x7136;&#x540E;&#x6211;&#x4EEC;&#x6307;&#x6D3E; <code>PoolboyApp.Worker</code> &#x6A21;&#x5757;&#x4F5C;&#x4E3A; <code>:worker_module</code>&#x3002;&#x8FDB;&#x7A0B;&#x6C60;&#x7684;&#x5927;&#x5C0F;&#x901A;&#x8FC7; <code>:size</code> &#x8BBE;&#x7F6E;&#x4E3A; 5&#x3002;&#x540C;&#x65F6;&#xFF0C;&#x901A;&#x8FC7;&#x914D;&#x7F6E; <code>:max_overflow</code> &#x9009;&#x9879;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x53EF;&#x4EE5;&#x8BA9;&#x8FDB;&#x7A0B;&#x6C60;&#x5728; worker &#x8FDB;&#x7A0B;&#x7E41;&#x5FD9;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6700;&#x591A;&#x521B;&#x5EFA;&#x4E24;&#x4E2A;&#x989D;&#x5916;&#x7684; worker&#x3002;<em>&#xFF08;<code>overflow</code> workers &#x5B8C;&#x6210;&#x5DE5;&#x4F5C;&#x540E;&#x4F1A;&#x88AB;&#x9500;&#x6BC1;&#x3002;&#xFF09;</em></p>
<p>&#x7136;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x628A; <code>:poolboy.child_spec/2</code> &#x51FD;&#x6570;&#x6DFB;&#x52A0;&#x5230; children &#x6570;&#x7EC4;&#x4E2D;&#xFF0C;&#x5B83;&#x5C31;&#x4F1A;&#x968F;&#x7740;&#x5E94;&#x7528;&#x7684;&#x542F;&#x52A8;&#x800C;&#x542F;&#x52A8;&#x3002;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x63A5;&#x6536;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;&#x8FDB;&#x7A0B;&#x6C60;&#x540D;&#x5B57;&#xFF0C;&#x548C;&#x5B83;&#x7684;&#x914D;&#x7F6E;&#x3002;  </p>
<h2 id="&#x521B;&#x5EFA;-worker">&#x521B;&#x5EFA; Worker</h2>
<p>Worker &#x6A21;&#x5757;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684; <code>GenServer</code>&#x3002;&#x5B83;&#x8BA1;&#x7B97;&#x5E73;&#x65B9;&#x6839;&#xFF0C;sleep &#x4E00;&#x79D2;&#xFF0C;&#x7136;&#x540E;&#x6253;&#x5370;&#x51FA; worker &#x7684; pid&#x3002;<code>lib/poolboy_app/worker.ex</code> &#x6587;&#x4EF6;&#x5982;&#x4E0B;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">PoolboyApp</span></span>.Worker <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> GenServer

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start_link</span></span>(<span class="hljs-number">_</span>) <span class="hljs-keyword">do</span>
    GenServer.start_link(__MODULE_<span class="hljs-number">_</span>, <span class="hljs-keyword">nil</span>, [])
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init</span></span>(<span class="hljs-number">_</span>) <span class="hljs-keyword">do</span>
    {<span class="hljs-symbol">:ok</span>, <span class="hljs-keyword">nil</span>}
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_call</span></span>({<span class="hljs-symbol">:square_root</span>, x}, _from, state) <span class="hljs-keyword">do</span>
    IO.puts(<span class="hljs-string">&quot;process <span class="hljs-subst">#{inspect(<span class="hljs-keyword">self</span>())}</span> calculating square root of <span class="hljs-subst">#{x}</span>&quot;</span>)
    <span class="hljs-symbol">:timer</span>.sleep(<span class="hljs-number">1000</span>)
    {<span class="hljs-symbol">:reply</span>, <span class="hljs-symbol">:math</span>.sqrt(x), state}
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<h2 id="poolboy-&#x7684;&#x4F7F;&#x7528;">Poolboy &#x7684;&#x4F7F;&#x7528;</h2>
<p>&#x65E2;&#x7136;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x6709;&#x4E86; <code>PoolboyApp.Worker</code>&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x6D4B;&#x8BD5; Poolboy &#x4E86;&#x3002;&#x6211;&#x4EEC;&#x5148;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#xFF0C;&#x4F7F;&#x7528; Poolboy &#x521B;&#x5EFA;&#x5E76;&#x53D1;&#x8FDB;&#x7A0B;&#x7684;&#x6A21;&#x5757;&#x3002;<code>:poolboy.transaction/3</code> &#x662F;&#x53EF;&#x4EE5;&#x548C; worker &#x8FDB;&#x7A0B;&#x6C60;&#x4EA4;&#x4E92;&#x7684;&#x51FD;&#x6570;&#x3002;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6; <code>lib/poolboy_app/test.ex</code> &#x5982;&#x4E0B;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">PoolboyApp</span></span>.Test <span class="hljs-keyword">do</span>
  <span class="hljs-variable">@timeout</span> <span class="hljs-number">60000</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start</span></span> <span class="hljs-keyword">do</span>
    <span class="hljs-number">1</span>..<span class="hljs-number">20</span>
    |&gt; Enum.map(<span class="hljs-keyword">fn</span> i -&gt; async_call_square_root(i) <span class="hljs-keyword">end</span>)
    |&gt; Enum.each(<span class="hljs-keyword">fn</span> task -&gt; await_and_inspect(task) <span class="hljs-keyword">end</span>)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">async_call_square_root</span></span>(i) <span class="hljs-keyword">do</span>
    Task.async(<span class="hljs-keyword">fn</span> -&gt;
      <span class="hljs-symbol">:poolboy</span>.transaction(
        <span class="hljs-symbol">:worker</span>,
        <span class="hljs-keyword">fn</span> pid -&gt; GenServer.call(pid, {<span class="hljs-symbol">:square_root</span>, i}) <span class="hljs-keyword">end</span>,
        <span class="hljs-variable">@timeout</span>
      )
    <span class="hljs-keyword">end</span>)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">await_and_inspect</span></span>(task), <span class="hljs-symbol">do:</span> task |&gt; Task.await(<span class="hljs-variable">@timeout</span>) |&gt; IO.inspect()
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#xFF0C;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#xFF1A;  </p>
<pre><code class="lang-shell">$ iex -S mix
</code></pre>
<pre><code class="lang-elixir">iex&gt; PoolboyApp.Test.start()
process <span class="hljs-comment">#PID&lt;0.182.0&gt; calculating square root of 7</span>
process <span class="hljs-comment">#PID&lt;0.181.0&gt; calculating square root of 6</span>
process <span class="hljs-comment">#PID&lt;0.157.0&gt; calculating square root of 2</span>
process <span class="hljs-comment">#PID&lt;0.155.0&gt; calculating square root of 4</span>
process <span class="hljs-comment">#PID&lt;0.154.0&gt; calculating square root of 5</span>
process <span class="hljs-comment">#PID&lt;0.158.0&gt; calculating square root of 1</span>
process <span class="hljs-comment">#PID&lt;0.156.0&gt; calculating square root of 3</span>
...
</code></pre>
<p>&#x5982;&#x679C;&#x8FDB;&#x7A0B;&#x6C60;&#x5DF2;&#x7ECF;&#x628A; worker &#x8FDB;&#x7A0B;&#x8017;&#x5C3D;&#x4E86;&#xFF0C;Poolboy &#x5C31;&#x4F1A;&#x5728;&#x9ED8;&#x8BA4;&#x7684;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#xFF08;5&#x79D2;&#xFF09;&#x540E;&#x4E22;&#x51FA;&#x8D85;&#x65F6;&#x9519;&#x8BEF;&#xFF0C;&#x5E76;&#x4E0D;&#x518D;&#x63A5;&#x6536;&#x4EFB;&#x4F55;&#x65B0;&#x7684;&#x8BF7;&#x6C42;&#x3002;&#x6211;&#x4EEC;&#x7684;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x5DF2;&#x7ECF;&#x628A;&#x9ED8;&#x8BA4;&#x7684;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#x6539;&#x6210;&#x4E00;&#x5206;&#x949F;&#xFF0C;&#x5C31;&#x662F;&#x4E3A;&#x4E86;&#x5C55;&#x793A;&#x5982;&#x4F55;&#x66F4;&#x6539;&#x9ED8;&#x8BA4;&#x7684;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#x3002;&#x4F60;&#x5982;&#x679C;&#x628A; <code>@timeout</code> &#x4FEE;&#x6539;&#x5230;&#x5C0F;&#x4E8E; 1000&#xFF0C;&#x5C31;&#x80FD;&#x89C2;&#x5BDF;&#x5230;&#x9519;&#x8BEF;&#x3002;  </p>
<p>&#x5373;&#x4FBF;&#x6211;&#x4EEC;&#x5C1D;&#x8BD5;&#x521B;&#x5EFA;&#x66F4;&#x591A;&#x7684;&#x8FDB;&#x7A0B; <em>&#xFF08;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x662F; 20 &#xFF09;</em>&#xFF0C;<code>:poolboy.transaction/3</code> &#x51FD;&#x6570;&#x8FD8;&#x662F;&#x4F1A;&#x9650;&#x5236;&#x6700;&#x5927;&#x7684;&#x8FDB;&#x7A0B;&#x6570;&#x4E3A; 5 <em>&#xFF08;&#x6709;&#x9700;&#x8981;&#x7684;&#x8BDD;&#x4F1A;&#x52A0;&#x4E0A;&#x4E24;&#x4E2A; overflow worker&#xFF09;</em>&#x3002;&#x6240;&#x6709;&#x7684;&#x8BF7;&#x6C42;&#x90FD;&#x4F1A;&#x901A;&#x8FC7;&#x8FDB;&#x7A0B;&#x6C60;&#x91CC;&#x9762;&#x7684; worker &#x6765;&#x5904;&#x7406;&#xFF0C;&#x800C;&#x4E0D;&#x4F1A;&#x4E3A;&#x6BCF;&#x4E2A;&#x65B0;&#x7684;&#x8BF7;&#x6C42;&#x521B;&#x5EFA;&#x65B0;&#x7684;&#x8FDB;&#x7A0B;&#x3002;  </p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="guardian.html" class="navigation navigation-prev " aria-label="Previous page: Guardian(基础)">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="benchee.html" class="navigation navigation-next " aria-label="Next page: Benchee">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.1.2","title":"Poolboy","level":"5.2","depth":1,"next":{"title":"Benchee","level":"5.3","depth":1,"path":"lessons/libraries/benchee.md","ref":"lessons/libraries/benchee.md","articles":[]},"previous":{"title":"Guardian(基础)","level":"5.1","depth":1,"path":"lessons/libraries/guardian.md","ref":"lessons/libraries/guardian.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/libraries/poolboy.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

